<template>
    <div>
        <cus-modal ref="modal" :title="form.name ? '替换菜肴' : '添加菜肴'" :width="1150" :closable="false"
            @on-visible-change="onVisibleChange">
            <div class="noticeBox">
                <Spin size="large" fix v-if="form.loading"> </Spin>
                <div style="display:flex">
                    <Input v-model="form.searchName" style="width: 200px" placeholder="请填写菜品名称" class="mr_24"
                        clearable></Input>
                    <cusButton class="mr_10" type="primary" @onClick="selectailocalfoodBydishApi">查询</cusButton>
                </div>
                <div style="margin-top:20px">
                    <Tabs type="card" @on-click="clickTbas" v-model="form.dishType">
                        <TabPane v-for="(item, index) in tabs" :key="index" :label="item.dishCategory"
                            :name="item.dishCategory">
                            <div class="dishesBox">
                                <div class="dishesItem" :class="item.select ? 'selectDishes' : ''"
                                    v-for="(item, index) in data" :key="index" @click="chooseItem(item,index)">{{
            item.dishName }}{{ item.select ? form.name ? '(替换)' : '(添加)' : '' }}</div>
                            </div>
                        </TabPane>
                    </Tabs>
                </div>
            </div>
            <div slot="footer">
                <slot name="footer">
                    <div class="modal-footer">
                        <cus-button size="small" class="mr_2" @onClick="onCancel">取消</cus-button>
                        <cus-button type="primary" size="small" @onClick="onOk" v-if="!form.name">确认</cus-button>
                        <cus-button type="primary" size="small" @onClick="onOk" v-if="form.name">替换</cus-button>
                    </div>
                </slot>
            </div>
        </cus-modal>
    </div>
</template>

<script>
import { selectailocaldishtype, selectailocalfoodBydish } from "@/api/organ/business";

export default {
    components: {},
    name: "addDishes",
    data() {
        return {
            form: { name: '', index: null, idx: null, key: null, dishType: null, searchName: '' },
            tabs: [],
            data: [], // 菜品列表
            selectItem: null,
             // 已选择的菜肴列表
             selectedList: []
        };
    },
    mounted() {

    },
    methods: {
        onVisibleChange(v) {
            if (!v) {
                this.form = { name: '', index: null, idx: null, key: null, dishType: null, searchName: '' };
                this.selectItem = null;
                this.data = [];
                thisl.selectedList = []
            }
        },
        show(index, idx, key) {
            this.form.index = index;
            this.form.idx = idx;
            this.form.key = key;
            this.$refs["modal"].show = true;
            this.selectailocaldishtypeApi().then(() => {
                this.selectailocalfoodBydishApi()
            });
        },
        onInit() {

        },
        // 确认发布
        onOk() {
            if (this.selectItem) {
                this.$emit('onAdd', this.form, this.selectItem.dishName);
                this.$refs["modal"].show = false;
            } else {
                this.$Message.warning(`请选择您要${this.form.name ? '替换' : '添加'}的菜品.`)
            }
        },
        // 确认
        onDelete() {
            this.$emit('onDelete', this.form);
            setTimeout(() => {
                this.$refs["modal"].show = false;
            }, 200)
        },
        //模态框取消
        onCancel() {
            this.$refs["modal"].show = false;
        },
        // 获取菜品分类列表
        selectailocaldishtypeApi() {
            return new Promise((resolve) => {
                selectailocaldishtype({}).then((res) => {
                    if (res.code == 1) {
                        this.tabs = res.data;
                        this.form.dishType = res.data[0].dishCategory;
                        resolve();
                    }
                });
            });
        },
        // 根据菜品分类获取菜品
        selectailocalfoodBydishApi() {
            this.form.loading = true;
            return new Promise((resolve) => {
                selectailocalfoodBydish({
                    dishCategory: this.form.dishType,
                    dishName: this.form.searchName,
                    size: -1,
                    current: 1
                }).then((res) => {
                    this.form.loading = false;
                    if (res.code == 1) {
                        this.data = res.data.list;
                        resolve();
                    }
                });
            });
        },
        // 类型切换
        clickTbas(name) {
            this.selectItem = null;
            this.selectailocalfoodBydishApi();
        },
        // 选中菜肴
        chooseItem(item,index) {
            let flag = this.selectedList.some(el => {
                return el[this.form.key] == item.dishName
            })
            if (flag) {
                this.$Message.info(`菜肴已选择`)
                return false
            }
            this.selectItem = this.data[index];
            this.data.forEach((item, idx) => {
                if (index == idx) {
                    this.$set(this.data[idx], 'select', true)
                } else {
                    this.$set(this.data[idx], 'select', false)
                }
            })
        }
    },
};
</script>

<style scoped lang="less">
// /deep/.ivu-modal-mask {
//     z-index: 4000!important;
// }
// /deep/.ivu-modal-wrap {
//     z-index: 4000!important;
// }
/*里面的代码可以根据自己需求去进行更改*/
/* 设置滚动条的样式 */
::-webkit-scrollbar {
    width: 4px;
}

/* 滚动槽 */
::-webkit-scrollbar-track {
    -webkit-box-shadow: inset006pxrgba(0, 0, 0, 0.3);
    border-radius: 10px;
}

/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.1);
    -webkit-box-shadow: inset006pxrgba(0, 0, 0, 0.5);
}

::-webkit-scrollbar-thumb:window-inactive {
    background: rgba(255, 255, 255, 0.4);
}

.noticeBox {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    line-height: 20px;
}

.dishesBox {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    align-content: start;
    border: 1px solid #d7dde4;
    padding: 10px;
    height: 200px;
    overflow: hidden;
    overflow-y: auto;
}

.dishesItem {
    margin: 0 16px 16px 0;
    color: #1890ff;
    cursor: pointer;
}

.descr {
    display: flex;
    flex-wrap: wrap;

    div {
        margin-bottom: 16px;
        margin-right: 30px;
    }
}

.selectDishes {
    color: aquamarine;
}
</style>
